<template>
  <div class="app-container">
    <div class="search-area" v-show="showSearch">
      <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" class="clearfix"
        label-width="auto">
        <el-form-item label="喇叭类型" prop="speakerType">
          <el-select v-model="queryParams.speakerType" clearable placeholder="请选择喇叭类型">
            <el-option label="小喇叭" :value="1" />
            <el-option label="大喇叭" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="使用状态" prop="status">
          <el-select v-model="queryParams.status" clearable placeholder="请选择使用状态">
            <el-option label="已使用" :value="1" />
            <el-option label="未使用" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="main-content">
      <el-row :gutter="10" class="mb24">
        <!-- <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['biz:speakerLog:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['biz:speakerLog:edit']">修改</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['biz:speakerLog:remove']">删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="speakerLogList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户id" align="center" prop="userId" v-if="columns.userId.visible" />
        <el-table-column label="喇叭id" align="center" prop="speakerId" v-if="columns.speakerId.visible" />
        <el-table-column label="喇叭类型" align="center" prop="speakerType" v-if="columns.speakerType.visible">
          <template slot-scope="scope">
            <span v-if="scope.row.speakerType === 1">小喇叭</span>
            <span v-else-if="scope.row.speakerType === 2">大喇叭</span>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column label="关联的订单id" align="center" prop="orderId" v-if="columns.orderId.visible" />
        <el-table-column label="喊话内容" align="center" prop="content" v-if="columns.content.visible" />
        <el-table-column label="使用状态" align="center" prop="status" v-if="columns.status.visible">
          <template slot-scope="scope">
            <span v-if="scope.row.status === 1">已使用</span>
            <span v-else-if="scope.row.status === 0">未使用</span>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-tickets" @click="handleUpdate(scope.row, true)"
              v-hasPermi="['biz:speakerLog:query']">查看</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['biz:speakerLog:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['biz:speakerLog:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>



    <!-- 添加或修改喇叭使用记录对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto" :disabled="disabled"
        :class="{ 'detail-disabled': disabled }">
        <el-form-item label="用户id(biz_user#id)" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id(biz_user#id)" />
        </el-form-item>
        <el-form-item label="喇叭id" prop="speakerId">
          <el-input v-model="form.speakerId" placeholder="请输入喇叭id" />
        </el-form-item>
        <el-form-item label="喇叭类型 1-小喇叭 2-大喇叭" prop="speakerType">
          <el-input v-model="form.speakerType" placeholder="请输入喇叭类型 1-小喇叭 2-大喇叭" />
        </el-form-item>
        <el-form-item label="关联的订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入关联的订单id" />
        </el-form-item>
        <el-form-item label="喊话内容">
          <editor v-model="form.content" :min-height="192" :readOnly="disabled" />
        </el-form-item>
        <el-form-item label="使用状态 1-已使用 0-未使用" prop="status">
          <el-input v-model="form.status" placeholder="请输入使用状态 1-已使用 0-未使用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!disabled">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSpeakerLog, getSpeakerLog, delSpeakerLog, addSpeakerLog, updateSpeakerLog } from "@/api/biz/speakerLog";

export default {
  name: "SpeakerLog",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 喇叭使用记录表格数据
      speakerLogList: [],
      // 弹出层标题
      title: "",
      // 是否禁止表单填写
      disabled: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        speakerType: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 列信息
      columns: {
        userId: { label: "用户id(biz_user#id)", visible: true },
        speakerId: { label: "喇叭id", visible: true },
        speakerType: { label: "喇叭类型 1-小喇叭 2-大喇叭", visible: true },
        orderId: { label: "关联的订单id", visible: true },
        content: { label: "喊话内容", visible: true },
        status: { label: "使用状态 1-已使用 0-未使用", visible: true },
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询喇叭使用记录列表 */
    getList() {
      this.loading = true;
      listSpeakerLog(this.formatParamsBatch(this.queryParams)).then(response => {
        this.speakerLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        speakerId: null,
        speakerType: null,
        orderId: null,
        content: null,
        status: 1,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.disabled = false;
      this.title = "添加喇叭使用记录";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row, readOnly) {
      this.reset();
      const id = row.id || this.ids
      getSpeakerLog(id).then(response => {
        this.form = response.data;
        this.disabled = readOnly ? true : false;
        this.title = readOnly ? "喇叭使用记录详情" : "修改喇叭使用记录";
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSpeakerLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpeakerLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除喇叭使用记录编号为"' + ids + '"的数据项？').then(function () {
        return delSpeakerLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/speakerLog/export', {
        ...this.queryParams
      }, `speakerLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.el-date-editor,
.el-select {
  width: 100%;
}
</style>
